package com.ben.ffy;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

//public class WellcomeSurfaceView extends Activity {
//
//	private Movie mMovie;
//	private long mMovieStart;
//
//	/** Called when the activity is first created. */
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//		super.onCreate(savedInstanceState);
//		setContentView(new CustomGifView(this));
//	}
//
//	class CustomGifView extends View {
//		public CustomGifView(Context context) {
//			super(context);
//			mMovie = Movie.decodeStream(getResources().openRawResource(
//					R.drawable.fish1));
//		}
//
//		public void onDraw(Canvas canvas) {
//			long now = android.os.SystemClock.uptimeMillis();
//
//			if (mMovieStart == 0) { // first time
//				mMovieStart = now;
//			}
//			if (mMovie != null) {
//
//				int dur = mMovie.duration();
//				if (dur == 0) {
//					dur = 3000;
//				}
//				int relTime = (int) ((now - mMovieStart) % dur);
//				mMovie.setTime(relTime);
//				mMovie.draw(canvas, 0, 0);
//				invalidate();
//			}
//		}
//	}
//
//}

public class WellcomeSurfaceView extends SurfaceView implements
		SurfaceHolder.Callback {
	MyActivity activity;
	Paint paint;
	int currentAlpha = 0;
	int sleepSpan = 50;
	Bitmap[] logos = new Bitmap[5];
	Bitmap currentLogo;
	int currentX;
	int currentY;

	public WellcomeSurfaceView(MyActivity activity) {
		super(activity);
		this.activity = activity;
		this.getHolder().addCallback(this);
		paint = new Paint();
		paint.setAntiAlias(true);
		logos[0] = BitmapFactory.decodeResource(getResources(),
				R.drawable.fish_1);
		logos[1] = BitmapFactory.decodeResource(getResources(),
				R.drawable.fish_2);
		logos[2] = BitmapFactory.decodeResource(getResources(),
				R.drawable.fish_3);
		logos[3] = BitmapFactory.decodeResource(getResources(),
				R.drawable.fish_4);
		logos[4] = BitmapFactory.decodeResource(getResources(),
				R.drawable.fish_1);
	}

	public void onDraw(Canvas canvas) {
		paint.setColor(Color.WHITE);
		paint.setAlpha(255);
		canvas.drawRect(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT,
				paint);
		if (currentLogo == null)
			return;
		int currentLogoWidth=currentLogo.getWidth();
		int  currentLogoHeight= currentLogo.getHeight();
		Matrix matrix = new Matrix();  
        matrix.postScale(Constant.SCREEN_WIDTH/currentLogoWidth, Constant.SCREEN_WIDTH/currentLogoWidth); 
        Bitmap dstbmp = Bitmap.createBitmap(currentLogo, 0, 0, currentLogoWidth, currentLogoHeight,matrix, true);
		paint.setAlpha(currentAlpha);
		canvas.drawBitmap(dstbmp, currentX, currentY, paint);

	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	@SuppressLint("WrongCall")
	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		new Thread() {
			public void run() {
				for (Bitmap bm : logos) {
					currentLogo = bm;

					currentX = 100;//Constant.SCREEN_WIDTH / 2 - bm.getWidth() / 2;
					currentY = 50;//Constant.SCREEN_HEIGHT / 2 - bm.getHeight() / 2;
					currentAlpha=255;
					SurfaceHolder myholderM = WellcomeSurfaceView.this
							.getHolder();
					Canvas canvasM = myholderM.lockCanvas();
					try {
						synchronized (myholderM) {
							onDraw(canvasM);
						}
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						if (canvasM != null) {
							myholderM.unlockCanvasAndPost(canvasM);
						}

					}
					try {
						Thread.sleep(200);
						Thread.sleep(sleepSpan);
					} catch (Exception e) {
						e.printStackTrace();
					}

					if (bm == logos[4]) 
					{
						for (int i = 255; i > -10; i = i - 10) {
							currentAlpha = i;
							if (currentAlpha < 0) {
								currentAlpha = 0;
							}
							SurfaceHolder myholder = WellcomeSurfaceView.this
									.getHolder();
							Canvas canvas = myholder.lockCanvas();
							try {
								synchronized (myholder) {
									onDraw(canvas);
								}
							} catch (Exception e) {
								e.printStackTrace();
							} finally {
								if (canvas != null) {
									myholder.unlockCanvasAndPost(canvas);
								}
							}

							try {
								if (i == 255) {
									Thread.sleep(1500);
								}
								Thread.sleep(sleepSpan);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
				activity.sendMessage(0);
			}
		}.start();

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub

	}
}
